using System;
using System.Data;
using System.Data.SqlClient;
using ExaminationSys.DataAccess.Entity;

namespace ExaminationSys.DataAccess.Repository
{
    public class UnitOfWork : IUnitOfWork
    {
        private readonly SqlConnection _context;
        private StudentRepository _studetns;
        private TeacherInfoRepository _teacherinfo;
        private NewsRepository _news;
        private NewsTypeRepository _newsTypeRepository;
        private Sys_DictionaryRepository _sys_DictionaryRepository;
        private Sys_UserRepository _sys_UserRepository;
        private SubSectionInfoRepository _subSectionInfoRepository;
        private SubSectionDetailInfoRepository _subSectionDetailInfoRepository;
        private SectionTypeRepository _sectionTypeRepository;
        private SectionInfoRepository _sectionInfoRepository;
        private SectionInformationRepository _sectionInformationRepository;
        private SectionLinkRepository _sectionLinkRepository;
        private SectionPaperRepository _sectionPaperRepository;
        private SectionDownloadRepository _sectionDownloadRepository;
        private PaperFeedBackCommentRepository _paperFeedBackCommentRepository;
        private PaperFeedbackRepository _paperFeedbackRepository;
        private PaperInfoRepository _paperInfoRepository;
        private PaperDetailAutoRepository _paperDetailAuto;
        private PaperSubjectQuestionRepository _paperSubjectQuestionRepository;
        private DBQuestionRepository _dBQuestion;
        private DBSubjectRepository _dBSubject;
        private DBSubjectQuestionRepository _dBSubjectQuestion;
        private DBAnswerRepository _dBAnswer;
        private StudentTestHistoryRepository _studentTestHistoryRepository;
        private StudentTestQuestionRepository _studentTestQuestionRepository;
        private LoginLogRepository _loginLog;
        private StudentLevelRepository _studentLevelRepository;
        private StudentRankRepository _studentRankService;
        private TeacherPrivilegeRelRepository _teacherPrivilegeRel;

        public UnitOfWork(SqlConnection context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("Context was not supplied");
            }

            _context = context;
        }

        #region IUnitOfWork Members

        public IRepository<TeacherPrivilegeRel> TeacherPrivilegeRel
        {
            get
            {
                if (_teacherPrivilegeRel == null)
                {
                    _teacherPrivilegeRel = new TeacherPrivilegeRelRepository(_context);
                }

                return _teacherPrivilegeRel;
            }
        }
        public IRepository<StudentRank> StudentRank
        {
            get
            {
                if (_studentRankService == null)
                {
                    _studentRankService = new StudentRankRepository(_context);
                }

                return _studentRankService;
            }
        }
        public IRepository<StudentLevel> StudentLevel
        {
            get
            {
                if (_studentLevelRepository == null)
                {
                    _studentLevelRepository = new StudentLevelRepository(_context);
                }

                return _studentLevelRepository;
            }
        }

        public IRepository<LoginLog> LoginLog
        {
            get
            {
                if (_loginLog == null)
                {
                    _loginLog = new LoginLogRepository(_context);
                }

                return _loginLog;
            }
        }

        public IRepository<Student> Students
        {
            get
            {
                if (_studetns == null)
                {
                    _studetns = new StudentRepository(_context);
                }

                return _studetns;
            }
        }

        public IRepository<TeacherInfo> TeacherInfo
        {
            get
            {
                if (_teacherinfo == null)
                {
                    _teacherinfo = new TeacherInfoRepository(_context);
                }

                return _teacherinfo;
            }
        }

        public IRepository<News> News
        {
            get
            {
                if (_news == null)
                {
                    _news = new NewsRepository(_context);
                }

                return _news;
            }
        }




        public IRepository<NewsType> NewsType
        {
            get
            {
                if (_newsTypeRepository == null)
                    _newsTypeRepository = new NewsTypeRepository(_context);
                return _newsTypeRepository;
            }
        }


        public void Dispose()
        {
            if (_context.State == ConnectionState.Open)
                _context.Close();
        }


        public IRepository<Sys_DictionaryEntity> Sys_DictionaryEntity
        {
            get
            {
                if (_sys_DictionaryRepository == null)
                {
                    _sys_DictionaryRepository = new Sys_DictionaryRepository(_context);
                }

                return _sys_DictionaryRepository;
            }

        }

        public IRepository<Sys_UserEntity> Sys_UserEntity
        {
            get
            {
                if (_sys_UserRepository == null)
                {
                    _sys_UserRepository = new Sys_UserRepository(_context);
                }

                return _sys_UserRepository;
            }

        }

        public IRepository<SubSectionInfo> SubSectionInfo
        {
            get
            {
                if (_subSectionInfoRepository == null)
                {
                    _subSectionInfoRepository = new SubSectionInfoRepository(_context);
                }

                return _subSectionInfoRepository;
            }

        }




        public IRepository<SubSectionDetailInfo> SubSectionDetailInfo
        {
            get
            {
                if (_subSectionDetailInfoRepository == null)
                {
                    _subSectionDetailInfoRepository = new SubSectionDetailInfoRepository(_context);
                }

                return _subSectionDetailInfoRepository;
            }

        }

        public IRepository<SectionType> SectionType
        {
            get
            {
                if (_sectionTypeRepository == null)
                {
                    _sectionTypeRepository = new SectionTypeRepository(_context);
                }

                return _sectionTypeRepository;
            }

        }


        public IRepository<SectionInfo> SectionInfo
        {
            get
            {
                if (_sectionInfoRepository == null)
                {
                    _sectionInfoRepository = new SectionInfoRepository(_context);
                }

                return _sectionInfoRepository;
            }

        }

        public IRepository<SectionInformation> SectionInformation
        {
            get
            {
                if (_sectionInformationRepository == null)
                {
                    _sectionInformationRepository = new SectionInformationRepository(_context);
                }

                return _sectionInformationRepository;
            }

        }

        public IRepository<SectionLink> SectionLink
        {
            get
            {
                if (_sectionLinkRepository == null)
                {
                    _sectionLinkRepository = new SectionLinkRepository(_context);
                }

                return _sectionLinkRepository;
            }

        }

        public IRepository<SectionPaper> SectionPaper
        {
            get
            {
                if (_sectionPaperRepository == null)
                {
                    _sectionPaperRepository = new SectionPaperRepository(_context);
                }

                return _sectionPaperRepository;
            }

        }

        public IRepository<SectionDownload> SectionDownload
        {
            get
            {
                if (_sectionDownloadRepository == null)
                {
                    _sectionDownloadRepository = new SectionDownloadRepository(_context);
                }

                return _sectionDownloadRepository;
            }

        }

        public IRepository<PaperFeedBackComment> PaperFeedBackComment
        {
            get
            {
                if (_paperFeedBackCommentRepository == null)
                {
                    _paperFeedBackCommentRepository = new PaperFeedBackCommentRepository(_context);
                }

                return _paperFeedBackCommentRepository;
            }

        }

        public IRepository<PaperFeedback> PaperFeedback
        {
            get
            {
                if (_paperFeedbackRepository == null)
                {
                    _paperFeedbackRepository = new PaperFeedbackRepository(_context);
                }

                return _paperFeedbackRepository;
            }

        }


        public IRepository<PaperInfo> PaperInfo
        {
            get
            {
                if (_paperInfoRepository == null)
                {
                    _paperInfoRepository = new PaperInfoRepository(_context);
                }

                return _paperInfoRepository;
            }
        }

        public IRepository<PaperDetailAuto> PaperDetailAuto
        {
            get
            {
                if (_paperDetailAuto == null)
                {
                    _paperDetailAuto = new PaperDetailAutoRepository(_context);
                }

                return _paperDetailAuto;
            }
        }

        public IRepository<PaperSubjectQuestion> PaperSubjectQuestion
        {
              get
            {
                if (_paperSubjectQuestionRepository == null)
                {
                    _paperSubjectQuestionRepository = new PaperSubjectQuestionRepository(_context);
                }

                return _paperSubjectQuestionRepository;
            }
            
        }

        public IRepository<DBQuestion> DBQuestion
        {
            get
            {
                if (_dBQuestion == null)
                {
                    _dBQuestion = new DBQuestionRepository(_context);
                }

                return _dBQuestion;
            }
        }

        public IRepository<DBSubject> DBSubject
        {
            get
            {
                if (_dBSubject == null)
                {
                    _dBSubject = new DBSubjectRepository(_context);
                }

                return _dBSubject;
            }
        }

        public IRepository<DBSubjectQuestion> DBSubjectQuestion
        {
            get
            {
                if (_dBSubjectQuestion == null)
                {
                    _dBSubjectQuestion = new DBSubjectQuestionRepository(_context);
                }

                return _dBSubjectQuestion;
            }
        }

        public IRepository<DBAnswer> DBAnswer
        {
            get
            {
                if (_dBAnswer == null)
                {
                    _dBAnswer = new DBAnswerRepository(_context);
                }

                return _dBAnswer;
            }
        }

        public IRepository<StudentTestHistory> StudentTestHistory
        {
            get
            {
                if (_studentTestHistoryRepository == null)
                {
                    _studentTestHistoryRepository = new StudentTestHistoryRepository(_context);
                }

                return _studentTestHistoryRepository;
            }
        }

        public IRepository<StudentTestQuestion> StudentTestQuestion
        {
            get
            {
                if (_studentTestQuestionRepository == null)
                {
                    _studentTestQuestionRepository = new StudentTestQuestionRepository(_context);
                }

                return _studentTestQuestionRepository;
            }
        }

        #endregion
    }
}